﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Model.Common;
using Chinaer.Core.Helper;

namespace Chinaer.Bll
{
    public class DingDan : Repository, IBll.IDingDan
    {
        IDao.IDingDanDAO dingDanDAO = new Chinaer.Dao.DingDanDAO();
        public List<DTO.DingDanDTO> GetDingDanByTime(DateTime beginTime, DateTime endTime, string dianPuID, string apiToken)
        {
            List<Model.Entities.Model_DingDan> listDingDan = dingDanDAO.GetDingDanByTime(beginTime, endTime, dianPuID);
            List<DTO.DingDanDTO> listDingDanDTO = new List<DTO.DingDanDTO>();
            foreach (var item in listDingDan)
            {
                DTO.DingDanDTO dingDanDTO = new DTO.DingDanDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, dingDanDTO);
                dingDanDTO.ApiToken = apiToken;
                //添加收货地址
                if (!string.IsNullOrEmpty(dingDanDTO.AddressID))
                {
                    IDao.IAddressDAO addressDAO = new Dao.AddressDAO();
                    Model.Entities.Model_Address model_Address = addressDAO.GetByID(dingDanDTO.AddressID);
                    if (model_Address != null)
                    {
                        dingDanDTO.AddressName = model_Address.Address;
                    }
                }
                //添加顾客名称
                if (!string.IsNullOrEmpty(dingDanDTO.CustomerID))
                {
                    IDao.ICustomerDAO customerDAO = new Dao.CustomerDAO();
                    Model.Entities.Model_Customer model_Customer = customerDAO.GetByID(dingDanDTO.CustomerID);
                    if (model_Customer != null)
                    {
                        dingDanDTO.CustomerName = model_Customer.LoginName;
                    }
                }
                //添加订单状态名称
                if (!string.IsNullOrEmpty(dingDanDTO.DingDanStatusID))
                {
                    IDao.IDingDanStatusDAO dingDanStatusDAO = new Dao.DingDanStatusDAO();
                    Model.Entities.Model_DingDanStatus model_DingDanStatus = dingDanStatusDAO.GetByID(dingDanDTO.DingDanStatusID);
                    if (model_DingDanStatus != null)
                    {
                        dingDanDTO.DingDanStatusName = model_DingDanStatus.Name;
                    }
                }
                listDingDanDTO.Add(dingDanDTO);
            }
            return listDingDanDTO;
        }

        public JsonResult Add(DTO.DingDanDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_DingDan model_DingDan = new Model.Entities.Model_DingDan();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_DingDan);
            if (string.IsNullOrEmpty(model_DingDan.ID) || model_DingDan.ID.Trim() == "")
            {
                model_DingDan.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            jsonResult.Success = dingDanDAO.Add(model_DingDan);
            return jsonResult;
        }

        public JsonResult Delete(DTO.DingDanDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_DingDan model_DingDan = new Model.Entities.Model_DingDan();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_DingDan);
            jsonResult.Success = dingDanDAO.Delete(model_DingDan);
            return jsonResult;
        }

        public JsonResult Delete(string id)
        {
            JsonResult jsonResult = new JsonResult();
            jsonResult.Success = dingDanDAO.Delete(id);
            return jsonResult;
        }

        public JsonResult Update(DTO.DingDanDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_DingDan model_DingDan = new Model.Entities.Model_DingDan();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_DingDan);
            jsonResult.Success = dingDanDAO.Update(model_DingDan);
            return jsonResult;
        }


        public DTO.DingDanDTO GetByID(string id)
        {
            Model.Entities.Model_DingDan model_DingDan = dingDanDAO.GetByID(id);
            DTO.DingDanDTO dingDanDTO = new DTO.DingDanDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_DingDan, dingDanDTO);
            return dingDanDTO;
        }
    }
}
